package net.guerlab.smart.platform.user.web.controller.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.guerlab.smart.platform.commons.util.BeanConvertUtils;
import net.guerlab.smart.platform.excel.ExcelUtils;
import net.guerlab.smart.platform.user.auth.UserContextHandler;
import net.guerlab.smart.platform.user.core.domain.OperationLogDTO;
import net.guerlab.smart.platform.user.core.exception.OperationLogInvalidException;
import net.guerlab.smart.platform.user.core.searchparams.OperationLogSearchParams;
import net.guerlab.smart.platform.user.service.service.OperationLogHandler;
import net.guerlab.smart.platform.user.service.service.OperationLogService;
import net.guerlab.smart.platform.user.service.service.SimpleOperationLogService;
import net.guerlab.smart.platform.user.web.excel.OperationLogExcelExport;
import net.guerlab.web.result.ListObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作记录
 *
 * @author guer
 */
@Api(tags = "操作记录")
@RestController("/user/operationLog")
@RequestMapping("/user/operationLog")
public class OperationLogController {

    private OperationLogService operationLogService;

    private SimpleOperationLogService simpleOperationLogService;

    private OperationLogHandler operationLogHandler;

    @ApiOperation("查询详情")
    @GetMapping("/{id}")
    public OperationLogDTO findOne(@ApiParam(value = "id", required = true) @PathVariable Long id) {
        return operationLogService.selectByIdOptional(id).orElseThrow(OperationLogInvalidException::new).toDTO();
    }

    @ApiOperation("查询列表")
    @GetMapping
    public ListObject<OperationLogDTO> findList(OperationLogSearchParams searchParams) {
        return BeanConvertUtils.toListObject(simpleOperationLogService.selectPage(searchParams));
    }

    @ApiOperation("查询全部")
    @GetMapping("/all")
    public List<OperationLogDTO> findAll(OperationLogSearchParams searchParams) {
        return BeanConvertUtils.toList(simpleOperationLogService.selectAll(searchParams));
    }

    @ApiOperation("导出Excel")
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, OperationLogSearchParams searchParams) {
        ExcelUtils.exportExcel(response, operationLogService.selectAll(searchParams), OperationLogExcelExport.class,
                "OperationLog-" + System.currentTimeMillis());
        operationLogHandler.add("导出操作记录列表excel", UserContextHandler.getUserId(), searchParams);
    }

    @Autowired
    public void setOperationLogService(OperationLogService operationLogService) {
        this.operationLogService = operationLogService;
    }

    @Autowired
    public void setSimpleOperationLogService(SimpleOperationLogService simpleOperationLogService) {
        this.simpleOperationLogService = simpleOperationLogService;
    }

    @Autowired
    public void setOperationLogHandler(OperationLogHandler operationLogHandler) {
        this.operationLogHandler = operationLogHandler;
    }
}
